// index.js
// 获取应用实例
const app = getApp()
import showGetUser from '../../utils/login';

Page({
  data: {
    list: [],
    banner: [],
    gridIcon: [],
    userInfo: app.globalData.userInfo,
    isLogin: app.globalData.isLogin
  },
  toList(e) {
    console.log(e.currentTarget.dataset.idx)
    const needType = e.currentTarget.dataset.idx +1
    wx.setStorageSync('needType', needType)
    wx.switchTab({
      url: '/pages/needList/needList',
    })
  },
  toNeed() {
    wx.switchTab({
      url: '/pages/needList/needList'
    })
  },



  init() {
    const banner = [
      '../../image/banner.png',
      '../../image/banner.png',
      '../../image/banner.png'
    ]
    this.setData({
      banner
    })
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'getNeed',
      data: {
        $url: 'list',
        status:'-1',
        searchKey:'',
        start: 0,
        count: 3
      }
    }).then(res => {
      console.log(res)
      this.setData({
        list: res.result.list.list
      })
      wx.hideLoading()
    })
  },

  showGetUser() {
    wx.showModal({
      title: '授权登录',
      content: '需要您的微信授权登录',
      success: res => {
        if (res.confirm) {
          this.getUserProfile()
        }
      }
    })
  },

  getUserProfile() {
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        console.log(res)
        this.onGetOpenid(res.userInfo)
      }
    })
  },

  onGetOpenid(res) {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {
        $url: 'user',
        ava: res.avatarUrl,
        name: res.nickName,
        sex: res.gender
      },
      success: res => {
        console.log(res)
        if (res.result) {
          wx.setStorageSync('isLogin', true)
          this.initUserData(res.result)
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000
          })
        }
      },
      fail: err => {
        console.error(err)
      }
    })
  },

  reLogin() {
    wx.cloud.callFunction({
      name: 'login',
      data: {
        $url: 'freshLogin'
      },
      success: res => {
        console.log(res)
        if (res.result) {
          const data = res.result.res.data[0]
          console.log(data)
          this.initUserData(data)
        }
      },
      fail: err => {
        console.error(err)
      }
    })
  },

  initUserData(data) {
    app.globalData.isLogin = true
    app.globalData.userInfo = data
    app.globalData.member = data.member
    app.globalData.langNo = data.langNo
    this.onLoad()
  },






  onLoad() {
    this.initPage()
    this.init()
    const isLogin = app.globalData.isLogin
    if (isLogin) {
      this.setData({
        isLogin,
        userInfo: app.globalData.userInfo
      })
    } else {
      //是否登录过
      const hasLogin = wx.getStorageSync('isLogin')
      if (hasLogin) {
        this.reLogin()
      } else {
        setTimeout(() => {
          this.showGetUser()
        }, 2000)
      }
    }
  },

  onShow() {
  },

  initPage() {
    //图标导航
    // const gridIcon = [
    //   {
    //     url: '/image/grid1.png',
    //     text: '专题/宣传/汇报',
    //   }, {
    //     url: '/image/grid2.png',
    //     text: '旁白/自白/独白',
    //   }, {
    //     url: '/image/grid3.png',
    //     text: '纪录片',
    //   }, {
    //     url: '/image/grid4.png',
    //     text: '促销广告',
    //   }, {
    //     url: '/image/grid5.png',
    //     text: '品牌广告',
    //   }, {
    //     url: '/image/grid6.png',
    //     text: '电影/电视剧',
    //   }, {
    //     url: '/image/grid7.png',
    //     text: '角色动画',
    //   }, {
    //     url: '/image/grid8.png',
    //     text: '新媒体配音',
    //   },
    // ]
    const gridIcon = [
      {
        url: '/image/index_03.png',
        text: '元宇宙',
      }, {
        url: '/image/index_05.png',
        text: '游戏',
      }, {
        url: '/image/index_07.png',
        text: '有声书',
      }, {
        url: '/image/index_09.png',
        text: '动漫',
      }
    ]
    this.setData({
      gridIcon
    })
  },
})
